package com.example.android.logging;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;

import com.example.android.gestures.AccRecord;

public class Logger {
	@SuppressLint("SdCardPath")
	static String defaultPath = "/sdcard/";
	public static Context CurrentContext;

	static void writeToFile(float[] values, String path) {
		StringBuilder stringBuilder = new StringBuilder().append("x = [");
		stringBuilder.append(values[0]);
		for (int i = 1; i < values.length; i++) {
			stringBuilder.append("," + values[i]);
		}

		stringBuilder.append("];");
	}

	static void writeToFile(ArrayList<AccRecord> accRecording) {
		StringBuilder xStringBuilder = new StringBuilder().append("x = [");
		StringBuilder yStringBuilder = new StringBuilder().append("y = [");
		StringBuilder zStringBuilder = new StringBuilder().append("z = [");
		StringBuilder tStringBuilder = new StringBuilder().append("t = [");
		if (accRecording.size() > 0) {
			AccRecord accRecord = accRecording.get(0);

			xStringBuilder.append(accRecord.getX());
			yStringBuilder.append(accRecord.getY());
			zStringBuilder.append(accRecord.getZ());
			tStringBuilder.append(accRecord.getTimestamp());
		}
		for (int i = 1; i < accRecording.size(); i++) {
			AccRecord accRecord = accRecording.get(i);

			xStringBuilder.append("," + accRecord.getX());
			yStringBuilder.append("," + accRecord.getY());
			zStringBuilder.append("," + accRecord.getZ());
			tStringBuilder.append("," + accRecord.getTimestamp());
		}
		xStringBuilder.append("];");
		yStringBuilder.append("];");
		zStringBuilder.append("];");
		tStringBuilder.append("];");

		writeToFile(defaultPath + "mysdfile.txt",
				xStringBuilder.toString() + "\n" + yStringBuilder.toString()
						+ "\n" + zStringBuilder.toString() + "\n"
						+ tStringBuilder.toString());
	}

	static void writeToFile(String path, String string) {
		try {
			File myFile = new File(path);
			myFile.createNewFile();
			FileOutputStream fOut = new FileOutputStream(myFile);
			OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
			myOutWriter.append(string);
			myOutWriter.close();
			fOut.close();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	public static void makeToast(String text) {
		if(text.length()>0)Toast.makeText(CurrentContext, text, Toast.LENGTH_SHORT).show();
	}
}
